package com.xls.pet.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryOrderByBuilder;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xls.pet.common.ResultCode;
import com.xls.pet.entity.Category;
import com.xls.pet.entity.Item;
import com.xls.pet.entity.ItemStock;
import com.xls.pet.entity.WorkOrder;
import com.xls.pet.entity.table.WorkOrderTableDef;
import com.xls.pet.exception.BusinessException;
import com.xls.pet.mapper.WorkOrderMapper;
import com.xls.pet.query.WorkOrderQuery;
import com.xls.pet.service.MinioService;
import com.xls.pet.service.WorkOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.xls.pet.entity.table.ItemStockTableDef.ITEM_STOCK;
import static com.xls.pet.entity.table.ItemTableDef.ITEM;
import static com.xls.pet.entity.table.WorkOrderTableDef.WORK_ORDER;

/**
 * 工单 服务层实现。
 *
 * @author xls
 * @since 2024-07-23
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements WorkOrderService {

    @Override
    public Page<WorkOrder> list(Integer pageNumber, Integer pageSize, Integer totalRow, WorkOrderQuery workOrderQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(WORK_ORDER.DAMAGE_STATUS.eq(workOrderQuery.getDamageStatus(), Objects::nonNull))
                .where(WORK_ORDER.INVOICE_STATUS.eq(workOrderQuery.getInvoiceStatus(), Objects::nonNull))
                .where(WORK_ORDER.VEHICLE_NAME.like(workOrderQuery.getVehicleName(), StringUtil::isNotBlank))
                .where(WORK_ORDER.PLATE_NUMBER.like(workOrderQuery.getPlateNumber(), StringUtil::isNotBlank))
                .where(WORK_ORDER.CUSTOMER_NAME.like(workOrderQuery.getCustomerName(), StringUtil::isNotBlank))
                .orderBy(WORK_ORDER.DAMAGE_STATUS, true)
                .orderBy(WORK_ORDER.INVOICE_STATUS, true)
                .orderBy(WORK_ORDER.CREATE_TIME, true);
        return this.page(Page.of(pageNumber, pageSize), queryWrapper);
    }
}
